/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eleric.rememome.activity;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.eleric.rememome.R;
import com.eleric.rememome.db.MemoDbAdaptor;
import com.eleric.rememome.db.factory.MemoDbAdaptorFactory;
import com.eleric.rememome.db.factory.impl.MemoDbAdaptorFactoryVer2;
import com.eleric.rememome.memo.Memo;
import com.eleric.rememome.type.FormatType;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MWaldman
 */
public class StatusActivity extends ListActivity
{

    protected MemoDbAdaptorFactory dbAdapterFactory;

    public StatusActivity()
    {
        super();
        dbAdapterFactory = new MemoDbAdaptorFactoryVer2();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        refreshList();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        ListView lv;

        lv = getListView();
        lv.setTextFilterEnabled(true);
    }

    private void refreshList()
    {
        ListAdapter listAdapter;
        String[] list;
        String type;
        MemoDbAdaptor dbAdaptor;

        dbAdaptor = dbAdapterFactory.createDbAdapter(this);
        dbAdaptor.open();

        type = "";
        list = getList(type);
        listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
        setListAdapter(listAdapter);
        dbAdaptor.close();
    }

    // Look up List in database based on type
    private String[] getList(String type)
    {
        String[] list;
        List<String> listObj;
        List<Memo> listMemo;
        MemoDbAdaptor dbAdaptor;
        dbAdaptor = dbAdapterFactory.createDbAdapter(this);

        dbAdaptor.open();
        listObj = new ArrayList<String>();
        listMemo = dbAdaptor.listMemos();

        for (Memo m : listMemo)
        {
            listObj.add(m.getFormatedMemo(FormatType.SIMPLE));
        }

        list = listObj.toArray(new String[0]);

        dbAdaptor.close();
        return list;
    }
}
